﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Containing all methods for searching and filtering
/// </summary>
public class Searching
{
    public Searching()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    /// <summary>
    /// search all project
    /// </summary>
    /// <returns>List project</returns>
    public List<Project> SearchProject()
    {
        var searchProject = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                            select item.Project;
        return searchProject as List<Project>;
    }

    /// <summary>
    /// search all project  user assigned
    /// code by Toan
    /// </summary>
    /// <param name="username">user id of searcher</param>
    /// <returns>List project</returns>
    public List<Project> SearchProject(string username)
    {
        var searchProject = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                            where item.Username == username
                            select item.Project;
        return searchProject as List<Project>;
    }

    /// <summary>
    /// search project of user assigned with description,name,project
    /// code by Toan
    /// </summary>
    /// <param name="username">user id of searcher</param>
    /// <param name="text">the text for search with name or description or customerMesseage</param>
    /// <returns>List project</returns>
    public List<Project> SearchProject(string username, string text)
    {
        var searchProject = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                            where item.Username == username && (item.Project.Description.Contains(text) || item.Project.Name.Contains(text) || item.Project.CustomMessage.Contains(text))
                            select item.Project;

        return searchProject as List<Project>;
    }

    /// <summary>
    /// search all the user in the programe
    /// code by Toan
    /// </summary>
    /// <returns>List user</returns>
    public List<User> SearchUser()
    {
        var users = ApplicationGlobalVariables.KillDaBugsDatabase.Users.Select(i => i);
        return users as List<User>;
    }

    /// <summary>
    /// search user with user name ,or full name, or email
    /// </summary>
    /// <param name="text">value want to search sampble is "abc@yahoo.com" ""</param>
    /// <returns>List Object</returns>
    public List<User> SearchUser(string text)
    {
        text = text.Trim();
        var users = from items in ApplicationGlobalVariables.KillDaBugsDatabase.Users
                    where items.Username.Contains(text) || items.Fullname.Contains(text) || items.Email.Contains(text) || items.Organization.Contains(text)

                    select items;
        return users as List<User>;
    }

    /// <summary>
    /// search user with their ROLE and value want to search
    /// </summary>
    /// <param name="text">value want to search </param>
    /// <param name="role">role of user want to search</param>
    /// <returns>List user</returns>
    public List<User> SearchUser(string text, int role)
    {
        text = text.Trim();
        var user = from items in ApplicationGlobalVariables.KillDaBugsDatabase.Users
                   where items.Role == role && (items.Username.Contains(text) || items.Fullname.Contains(text) || items.Email.Contains(text) || items.Organization.Contains(text))
                   select items;
        return user as List<User>;
    }

    /// <summary>
    /// search user with their ROLE
    /// </summary>
    /// <param name="role">role of user want to search</param>
    /// <returns>List user</returns>
    public List<User> SearchUser(int role)
    {
        var user = from items in ApplicationGlobalVariables.KillDaBugsDatabase.Users
                   where items.Role == role
                   select items;
        return user as List<User>;
    }

    /// <summary>
    /// search all the user in project
    /// </summary>
    /// <param name="projectID">ID of project want to search </param>
    /// <returns>List user</returns>
    public List<User> SearchUserInProject(int projectID)
    {
        var users = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                    where item.ProjectId == projectID
                    select item.User;

        return users as List<User>;
    }

    /// <summary>
    /// search all the user in project with with Role
    /// </summary>
    /// <param name="projectID">ID of project want to search </param>
    /// <param name="role">role of user</param>
    /// <returns>List user</returns>
    public List<User> SearchUserInProject(int projectID, int role)
    {
        var users = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                    where item.ProjectId == projectID && item.User.Role == role
                    select item.User;

        return users as List<User>;
    }

    /// <summary>
    /// search All bug user Assigned
    /// </summary>
    /// <param name="userName">user name of user</param>
    /// <returns>List Bug</returns>
    public List<Bug> SearchBugAsign(string userName)
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                  where item.Username == userName
                  select item.Bug;

        return bug as List<Bug>;
    }

    /// <summary>
    /// search All the comment in a bug
    /// </summary>
    /// <param name="bugID">ID of bug</param>
    /// <returns>List BugComment</returns>
    public List<BugComment> SearchCommentsOnBug(int bugID)
    {
        var comment = from item in ApplicationGlobalVariables.KillDaBugsDatabase.BugComments
                      where item.BugId == bugID
                      select item;
        return comment as List<BugComment>;
    }

    /// <summary>
    /// search All assigned bug Status
    /// </summary>
    /// <param name="userName">user name assigned in bug</param>
    /// <param name="status"></param>
    /// <returns>List Bug</returns>
    public List<Bug> SearchBugStatus(string userName, int status)
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                  where item.Username == userName && item.Bug.Status == status
                  select item.Bug;
        return bug as List<Bug>;
    }

    /// <summary>
    /// search All assigned bug with Resolution
    /// </summary>
    /// <param name="username">user name assigned  in bug</param>
    /// <param name="resolution">resolution of bug</param>
    /// <returns></returns>
    public List<Bug> SearchBugResolution(string userName, int resolution)
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                  where item.Username == userName && item.Bug.Resolution == resolution
                  select item.Bug;
        return bug as List<Bug>;
    }

    /// <summary>
    /// search All assigned bug with Priority
    /// </summary>
    /// <param name="username">user name assigned  in bug</param>
    /// <param name="Priority">Priority of bug</param>
    /// <returns></returns>
    public List<Bug> SearchBugPriority(string userName, int priority)
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                  where item.Username == userName && item.Bug.Priority == priority
                  select item.Bug;
        return bug as List<Bug>;
    }

    /// <summary>
    /// search All assigned bug with Type
    /// </summary>
    /// <param name="username">user name assigned  in bug</param>
    /// <param name="Priority">Type of bug</param>
    /// <returns></returns>
    public List<Bug> SearchBugType(string userName, int type)
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                  where item.Username == userName && item.Bug.Type == type
                  select item.Bug;
        return bug as List<Bug>;
    }

    /// <summary>
    /// search All assigned bug with Severity
    /// </summary>
    /// <param name="username">user name assigned  in bug</param>
    /// <param name="Priority">Severity of bug</param>
    /// <returns></returns>
    public List<Bug> SearchBugSeverity(string userName, int severity)
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                  where item.Username == userName && item.Bug.Severity == severity
                  select item.Bug;
        return bug as List<Bug>;
    }

    /// <summary>
    /// search All assigned bug with Reproducability
    /// </summary>
    /// <param name="username">user name assigned  in bug</param>
    /// <param name="reproducability">reproducability of bug</param>
    /// <returns></returns>
    public List<Bug> SearchBugReproducability(string userName, int reproducability)
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                  where item.Username == userName && item.Bug.Reproducability == reproducability
                  select item.Bug;
        return bug as List<Bug>;
    }

    /// <summary>
    /// serach all update
    /// </summary>
    /// <returns>list Bug</returns>
    public List<Bug> SearchRecentBugs()
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs
                  orderby item.LastUpdatedDate descending
                  select item;
        return bug as List<Bug>;
    }

    /// <summary>
    /// serach all update assigned
    /// </summary>
    /// <returns>list Bug</returns>
    public List<Bug> SearchRecentBugs(string username)
    {
        var bug = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                  where item.Username == username
                  orderby item.Bug.LastUpdatedDate descending
                  select item.User;
        return bug as List<Bug>;
    }
}